/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package demo.org.springframework.integration.samples.tcpclientserver;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent;
import org.springframework.integration.ip.tcp.serializer.TcpDeserializationExceptionEvent;
import org.springframework.messaging.Message;


/**
 * Simple service that receives data in a byte array,
 * converts it to a String and appends it to 'echo:'.
 *
 * @author Gary Russell
 *
 */
public class EchoService {

	public String test(String input) {
		System.out.println("EchoService input string = " + input);
		if ("FAIL".equals(input)) {
			throw new RuntimeException("Failure Demonstration");
		}
		return "echo:" + input;
	}

	public void test1(String content) {
		System.out.println("EchoService content = " + content);
	}
	
	/**
	 * Spring Integration 会自动匹配类型的
	 * 
	 * */
	public CustomOrder test(CustomOrder c) {
		System.out.println(Thread.currentThread() + ":" + ToStringBuilder.reflectionToString(c));
		return c;
	}
	
	public void processTCPConnectionEvents(Message<?> message) {
		if(message.getPayload() instanceof TcpConnectionOpenEvent){
			TcpConnectionOpenEvent tcpConnectionOpenEvent=(TcpConnectionOpenEvent)message.getPayload();
			System.out.println((Thread.currentThread().getName() + "监听到TcpConnectionOpenEvent："+ tcpConnectionOpenEvent.toString()));
		}else if (message.getPayload() instanceof TcpConnectionCloseEvent){
			TcpConnectionCloseEvent tcpConnectionCloseEvent=(TcpConnectionCloseEvent)message.getPayload();
			System.out.println((Thread.currentThread().getName() + "监听到TcpConnectionCloseEvent："+ tcpConnectionCloseEvent.toString()));
		}else if (message.getPayload() instanceof TcpConnectionExceptionEvent) {
			TcpConnectionExceptionEvent tcpConnectionExceptionEvent = (TcpConnectionExceptionEvent) message.getPayload();
			System.out.println(Thread.currentThread().getName() + "监听到TcpConnectionExceptionEvent："+message);
			System.out.println(tcpConnectionExceptionEvent.getCause());
		} else if (message.getPayload() instanceof TcpDeserializationExceptionEvent) {
			TcpDeserializationExceptionEvent tcpDeserializationExceptionEvent = (TcpDeserializationExceptionEvent) message.getPayload();
			System.out.println(Thread.currentThread().getName() + "监听到TcpDeserializationExceptionEvent："
					+"原因为"+tcpDeserializationExceptionEvent.getCause()
					+"，buffer中产生异常的offser为"+tcpDeserializationExceptionEvent.getOffset()
					+"，具体信息如下："+message);
			System.out.println(tcpDeserializationExceptionEvent.getCause());
		}else{
			System.out.println(Thread.currentThread().getName() + "监听到一个未知类型的IpIntegrationEvent："+message);
		}
	}
}